﻿import React, { Component } from 'react';
import { Tabs,DatePicker,Button ,message,} from 'antd';
import moment from 'moment';
import NetUitl from '../../utils/HttpUtil';
import {oGetVars}from '../../utils/urlUnescapeForOes';
import "./page.scss";
import TeamPage from './TeamPage'
import TeamBranch from './TeamBranch'
import TeamBranchT from './TeamBranchT'
import NoteModal from '../../components/NoteModal'
import { func } from 'prop-types';

const { TabPane } = Tabs;
const dataList = [
    {key:8,name:"研发整体"},
    {key:1,name:"数据与咨询事业部研发中心"},
    {key:2,name:"软件事业部产品研发部"},
    {key:3,name:"软件事业部研发支持部"},
    {key:4,name:"研究院中台研发中心"},
    {key:5,name:"供应链事业部研发中心"},
    {key:6,name:"政府解决方案事业部研发中心"},
    {key:7,name:"HIA运营中心研发组"},
]

class NoneData extends Component {
    render() {
        return <div>暂无数据</div>
    }
}

let oGetVarsfn = (url)=>{
    let arr =  window.location.href.split("?")
    console.log(arr)
   if(arr.length>1){
       let narr = arr[arr.length-1].split("&")
       let narr1 = narr[0].split("=")
       let narr2 = narr[1].split("=")
       return {
           id:narr1[1],
           date:narr2[1]
       }
   }
   return {
       id:8,
       date:""
   }
}

class Page1 extends Component {
    constructor(props) {
        super(props);
        this.state={
            id:8,
            tabsData:dataList,
            date:"2020-02-15",
            modelT: null,
            data:{},
            dataNote:{},
            searchDate:"",
            isDuli:false,
            noteVisible:false
        }
        this.callback=this.callback.bind(this)
        this.onChange=this.onChange.bind(this)
        this.callbackContent=this.callbackContent.bind(this)
        this.searchDateClick=this.searchDateClick.bind(this)
    }
      callback(id) {
        console.log(id,"ddddd");
        const {date,} = this.state
        this.callbackContent(id,date)
      
      }
      callbackContent(ids,date){
        let self = this;
        let id = ids?ids:8
        if(id==8){
            let url = `/dailyReport/all?date=${date}`
            console.log(url,"-----")

            NetUitl.get(url, null, function (data) {
                if (data && data.msgCode == 200) {
                                console.log(data.data)
                                
                    self.setState({ data: data.data ,modelT:<TeamPage data={data.data} date={date} />,id:id,date:date,dataNote:data.data})
                }else{
                    self.setState({ data: {} ,modelT:<NoneData />,id:id,date:date})
                }
                
            }, function (data) {
                console.log(data)
            })

          }else if(id==3){
            let url = `/dailyReport/search?date=${date}&teamId=${id}`

            NetUitl.get(url, null, function (data) {
                if (data && data.msgCode == 200) {
                    console.log(data.data)
                    data.data["team"]["teamName"]= data.data.top ? data.data.top.teamName:""
                    self.setState({ data: data.data ,modelT:<TeamBranchT data={data.data} date={date} />,id:id,date:date})
                   
                }else{
                    self.setState({ data: {} ,modelT:<NoneData />,id:id,date:date})
                }
                
            }, function (data) {
                console.log(data)
            })
          }else{
            let url = `/dailyReport/search?date=${date}&teamId=${id}`
            NetUitl.get(url, null, function (data) {
                if (data && data.msgCode == 200) {
                    console.log(data.data)
                    data.data["team"]["teamName"]= data.data.top ? data.data.top.teamName:""

                    self.setState({ data: data.data ,modelT:<TeamBranch id={id} data={data.data} date={date} />,id:id,date:date})
                }else{
                    self.setState({ data: {} ,modelT:<NoneData />,id:id,date:date})
                }
                
            }, function (data) {
                console.log(data)
            })
          }
      }
      onChange(date, dateString) {
        console.log(date, "ddd",moment(date).format('YYYY-MM-DD'));
        let searchDate = moment(date).format('YYYY-MM-DD')
        this.setState({searchDate:searchDate})
      }
      searchDateClick() {
       const {searchDate,id} = this.state
       console.log(searchDate,"searchDate")
        
        this.callbackContent(id,searchDate)
      }
      componentWillMount(){
          let oGetVars = oGetVarsfn()
        let date = oGetVars.date?moment(oGetVars.date).format('YYYY-MM-DD'):moment().format('YYYY-MM-DD')
        let id =oGetVars.id?oGetVars.id:0
        console.log(oGetVars,'----------',window.location,id,window.location.href.split("&"),this.props.location)
        const { location} = this.props;

        if(id && (!location.query || !location)){
            if(id!=8){

                let tabsData = []
                tabsData.push(dataList[0])
                tabsData.push(dataList[id])
      
                this.setState({id:id,tabsData:tabsData,date:date,isDuli:true})
            }
        }
      }
      componentDidMount(){
        let oGetVars = oGetVarsfn()

          const { location} = this.props;
          console.log(oGetVars.id,oGetVars.date,'----------',window.location, this.props.location)

          let date = (oGetVars.date && (!location.query || !location))?moment(oGetVars.date).format('YYYY-MM-DD'):moment().format('YYYY-MM-DD')
          let id =(oGetVars.id && (!location.query || !location))?oGetVars.id:0

          // let date = oGetVars.date?moment(oGetVars.date).format('YYYY-MM-DD'):moment().format('YYYY-MM-DD')
          // let id =oGetVars.id?oGetVars.id:0
          this.callbackContent(id,date)
      }
      handlecancel=()=>{
        window.location.href="/index.html#/"
    }

    handleNote = () =>{
        this.setState({
            noteVisible:true
        })
    }
    handleNoteOK = (v) =>{
        const {dataNote,date} = this.state
        dataNote.remark = v
        this.setState({
            noteVisible:false,
            dataNote
        })
        let url = `/totalRemark/saveOrUpdate`
        const obj = {
            reportDate:date,
            remark:v,
        }
        var aa = this
        var {id}=this.state
        var date1 = this.state.date
        NetUitl.post(url, obj, function (data) {
            if (data && data.msgCode == 200) {
                aa.callbackContent(id,date1)
            }else{
                message.error('保存失败！')
            }

        }, function (data) {
            console.log(data)
        })
    }
    handleNoteCancel = (v) =>{
        this.setState({
            noteVisible:false
        })
    }
    render() {
        const {id,tabsData,modelT,isDuli} = this.state
        console.log('id',id)
        return (
            <div className="pc-contain">

                 <div className="select-body">
                        {!isDuli? <div> 日期： <DatePicker onChange={this.onChange} placeholder="请输入日期进行查询" /></div>  :null}
                        <div>
                            <Button  onClick={this.handlecancel} style={{marginRight:8}}>返回</Button>
                            <Button onClick={this.handleNote} style={{marginRight:8}}>备注</Button>
                        {!isDuli?  <Button type="primary" onClick={this.searchDateClick}>查询</Button>:null}
                        </div>
                    </div>

                 <div>
                    <Tabs defaultActiveKey={`${id}`} onChange={this.callback}>
                        {
                            tabsData.map((item,i)=>{
                                return <TabPane tab={item.name} key={item.key} tabPosition={'bottom'}>
                                    
                                {
                                    item.key==id?modelT:null

                                }
                                </TabPane>
                            })
                        }
                    </Tabs>
                    
                    </div>
                <NoteModal
                    title={'研发整体备注'}
                    noteVisible={this.state.noteVisible}
                    allData={this.state.dataNote}
                    handleNoteOK={this.handleNoteOK}
                    handleNoteCancel={this.handleNoteCancel}
                />

            </div>
        );
    }
}
                        
// {
//     item.key==id?
//     <div>
//         {modelT}
//         <NoteModal
//             title={'研发整体备注'}
//             noteVisible={this.state.noteVisible}
//             allData={this.state.dataNote}
//             handleNoteOK={this.handleNoteOK}
//             handleNoteCancel={this.handleNoteCancel}
//         />
//     </div>
//     :null

// }
export default Page1;
